using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminEntityListing : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected DataObj CurPageDataObj = new DataObj();

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        // every page load, need to clear the messagebox
        this.Table_MessageBox.Visible = false;

        if (!IsPostBack)
        {
            this.SwitchLanguage();
            this.PageComponentSetup();

            this.LoadData();

            if (this.ResultTable != null)
            {
                DataView ResultView = new DataView(this.ResultTable);
                ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
                this.BindData(ResultView);
            }
        } 
    }

    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView);
        }
    }
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView);
        }
    }
    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                /*
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");

                this.Item_DateCreated = CurLanguageFactory.DisplayValue("ItemDateCreated");
                this.Item_ClaimID = CurLanguageFactory.DisplayValue("ItemClaimID");
                this.Item_Purpose = CurLanguageFactory.DisplayValue("ItemPurpose");
                this.Item_Currency = CurLanguageFactory.DisplayValue("ItemCurrency");
                this.Item_Amount = CurLanguageFactory.DisplayValue("ItemAmount");
                this.Item_Status = CurLanguageFactory.DisplayValue("ItemStatus");
                this.Item_PaymentDate = CurLanguageFactory.DisplayValue("ItemPaymentDate");

                this.Status_All = CurLanguageFactory.DisplayValue("StatusAll");
                this.Status_Submitted = CurLanguageFactory.DisplayValue("StatusSubmitted");
                this.Status_Approved = CurLanguageFactory.DisplayValue("StatusApproved");
                this.Status_Escalated = CurLanguageFactory.DisplayValue("StatusEscalated");
                this.Status_Processed = CurLanguageFactory.DisplayValue("StatusProcessed");
                this.Status_Paid = CurLanguageFactory.DisplayValue("StatusPaid");
                this.Status_RejectedByApprover = CurLanguageFactory.DisplayValue("StatusRejectedByApprover");
                this.Status_RejectedByFinance = CurLanguageFactory.DisplayValue("StatusRejectedByFinance");

                this.SortByASC = CurLanguageFactory.DisplayValue("SortOrderASC");
                this.SortByDESC = CurLanguageFactory.DisplayValue("SortOrderDESC");
                */

                this.DropDownList_SortBy.Items.Add(new ListItem("Order", this.CurPageDataObj.Order));
                this.DropDownList_SortBy.Items.Add(new ListItem("Name", this.CurPageDataObj.DisplayName));
                this.DropDownList_SortBy.Items.Add(new ListItem("AD BPCS", this.CurPageDataObj.BPCS));                

                this.DropDownList_SortOrder.Items.Add(new ListItem("Ascending", "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem("Descending", "DESC"));
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminAuthorizedListing.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage

    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
    }

    protected void ShowErrorMessage(ArrayList ErrorMessageList)
    {
        if (ErrorMessageList != null)
        {
            StringBuilder ErrorMessageBuilder = new StringBuilder("<ul>");

            for (int x = 0; x < ErrorMessageList.Count; x++)
            {
                ErrorMessageBuilder.AppendLine("<li>" + ErrorMessageList[x] + "</li>");
            }

            ErrorMessageBuilder.AppendLine("</ul>");

            this.Table_MessageBox.Visible = true;
            this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessageBuilder.ToString();
        }
    }

    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (EntityManager CurEntityManager = new EntityManager(this.CurrentPrivateSession))
        {
            MasterBaseCollection EntityCollection = CurEntityManager.GetAllEntity();

            if (EntityCollection != null)
            {
                this.ResultTable = new DataTable();
                
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EntityID, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DisplayName, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Order, Type.GetType("System.Int32")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CCPayment, Type.GetType("System.Int32")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.CCPaymentInString, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.BPCS, Type.GetType("System.String")));
                this.ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.EditLink, Type.GetType("System.String")));

                for (int x = 0; x < EntityCollection.Count; x++)
                {
                    DataRow ResultRow = this.ResultTable.NewRow();

                    Entity CurEntity = (Entity)EntityCollection[x];

                    ResultRow[this.CurPageDataObj.EntityID] = CurEntity.InternalID;
                    ResultRow[this.CurPageDataObj.DisplayName] = CurEntity.DisplayName;
                    ResultRow[this.CurPageDataObj.Order] = CurEntity.Order;
                    ResultRow[this.CurPageDataObj.CCPayment] = CreditCardManager.GetCreditCardPaymentTypeInCode(CurEntity.CreditCardPaymentType);
                    ResultRow[this.CurPageDataObj.CCPaymentInString] = CreditCardManager.GetCreditCardPaymentTypeInString(CurEntity.CreditCardPaymentType); ;
                    ResultRow[this.CurPageDataObj.BPCS] = CurEntity.BPCS;
                    ResultRow[this.CurPageDataObj.EditLink] = "<a href='GonzalesAdminEntityDetail.aspx?id=" + CurEntity.InternalID + "'  class='GridViewLink'>Edit</a>";                    

                    this.ResultTable.Rows.Add(ResultRow);
                }
                
            }
            else
            {
                this.ShowErrorMessage("Data Error loading");
                this.CurLogManager.LogError("[GonzalesAdminEntityListing.LoadData] : CurEntityManager.GetAllEntity() : " + CurEntityManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
            }
        }                 
    }

    #endregion
   
    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string EntityID = "EntityID";
        public readonly string DisplayName = "DisplayName";
        public readonly string BPCS = "BPCS";
        public readonly string CCPayment = "CCPayment";
        public readonly string CCPaymentInString = "CCPaymentInString";
        public readonly string Order = "Order";
        public readonly string EditLink = "EditLink";

    }

    #endregion
}
